package com.yanqu.road.server.cmd.activity.uniongroup;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_UNION_ACTIVITY_GROUP_GET_UNION_RANK, desc = "活动范围内商会排名")
public class UnionActivityGroupGetUnionRankCmd extends AbstractCommand {

    private UnionActivityGroupProto.UnionActivityGroupGetUnionRankReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = UnionActivityGroupProto.UnionActivityGroupGetUnionRankReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(reqMsg.getActivityId());
        if (activityInfo == null) {
            activityInfo = UnionWarActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        }
        if (activityInfo == null) {
            activityInfo = StrongholdActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        }
        if (activityInfo == null){
            activityInfo = OreWarActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        }
        if (activityInfo == null){
            activityInfo = TradeWarActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        }
        if (activityInfo == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        player.sendPacket(CrossProtocol.C_UNION_ACTIVITY_GROUP_GET_UNION_RANK, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            UnionActivityGroupProto.UnionActivityGroupGetUnionRankRespMsg.Builder builder = UnionActivityGroupProto.UnionActivityGroupGetUnionRankRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_UNION_ACTIVITY_GROUP_GET_UNION_RANK, builder);
        }
    }
}
